package org.example.huawei;

/**
 * @version 1.0.0
 * @author: lynn
 * @description: ABC 三个线程依次启动,join 实现
 * @date: 2021/6/4 10:55
 */
public class OrderStartThreadJoin {
    /*public static void main(String[] args) {
        Thread t1=new Thread(new Worker(null));t1.setName("A");
        Thread t2=new Thread(new Worker(t1));t2.setName("B");
        Thread t3=new Thread(new Worker(t2));t3.setName("C");
        t1.start();t2.start();t3.start();
    }
    static  class  Worker implements Runnable{
        private Thread thread;
        Worker(Thread thread){
            this.thread=thread;
        }
        @Override
        public void run() {
            if (thread != null ){ // 如果有线程,就 join 进来的线程执行完之后，,没有的话就直接输出
                try {
                    thread.join();
                    System.out.println("thread start : " + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println("thread start : " + Thread.currentThread().getName());
            }
        }
    }*/

    public static void main(String[] args) {
        Thread t1=new Thread(new Work(null));
        t1.setName("A");
        Thread t2=new Thread(new Work(t1));
        t2.setName("B");
        Thread t3=new Thread(new Work(t2));
        t3.setName("C");
        t1.start();
        t2.start();
        t3.start();


    }
    static class Work implements Runnable{

        private Thread thread;
        Work(Thread thread){
            this.thread=thread;
        }
        @Override
        public void run() {
            if (thread!=null){
                try {
                    thread.join();
                    System.out.println("this execute thread name: "+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println("this execute thread name: "+Thread.currentThread().getName());
            }
        }
    }
}
